import Inject from "@entity-access/entity-access/dist/di/di.js"; import { Route } from "@entity-access/server-pages/dist/core/Route.js"; import Page from "@entity-access/server-pages/dist/Page.js"; import SocialMailContext from "../../../../../server/model/SocialMailContext.js"; import StoreConfigService from "../../../../../server/services/store/StoreConfigService.js"; import WebSiteContentService from "../../../../../server/services/files/WebSiteContentService.js"; import StorePaymentGatewayService from "../../../../../server/services/store/StorePaymentGatewayService.js"; import EntityAccessError from "@entity-access/entity-access/dist/common/EntityAccessError.js"; import { storeAuthRedirect } from "../../storeAuthRedirect.js"; @storeAuthRedirect export default class extends Page { @Route productID; @Inject db: SocialMailContext; @Inject storeConfigService: StoreConfigService; @Inject siteContentService: WebSiteContentService; @Inject pgs: StorePaymentGatewayService; async run() { // only if logged in... // 1. Create order // 2. Route to Checkout let [ currency ] = this.childPath as any[]; const w = await this.siteContentService.getWebSite(this.request.hostName); const storeID = w.folderID; if (!currency) { currency = w.currency; } // todo: optimization // do not create new orders if new order for same item exists // with only a single item... const { productID } = this; const { price } = await this.storeConfigService.getPrice(productID, currency, this.request.hostName, storeID); const { userID } = this.sessionUser; const contact = await this.db.storeAccounts.where({userID }, (x, p) => x.userID === p.userID) .include((x) => x.addresses) .first() ?? EntityAccessError.throw(`Please signup as a store user instead of backend user.`); const subscription = this.db.storeSubscriptions.add({ storeID, buyerID: contact.accountID, status: "pending", itemPrice: price }); await this.db.saveChanges(); const orderID = subscription.currentOrderID; // let us pay for the order... return this.redirect(`/store/checkout/${orderID}`); } }